package com.yanqu.road.dao.impl.activity.share;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.ShareInvitationData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class InvitedDataDaoImpl extends TempDataObjectDao<ShareInvitationData> {

    public static String getShareInvitationDataSql = "select * from `t_u_invite_user_data` ";
    public Map<Long, List<ShareInvitationData>> getShareInvitationData() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<ShareInvitationData>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getShareInvitationDataSql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                ShareInvitationData data = parse(rs);
                long userId = data.getUserId();
                if (!dataMap.containsKey(userId)) {
                    dataMap.put(userId, new ArrayList<>());
                }
                dataMap.get(userId).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateShareInvitationDataSql = "update `t_u_invite_user_data` set invitationServerId=?,invitationUserId=?,earnSpeed=?,vip=?,registerTime=? where userId=? and openId=?";
    @Override
    protected boolean updateData(ShareInvitationData shareInvitationData) {
        shareInvitationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getInvitationServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getInvitationUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, shareInvitationData.getEarnSpeed().toString()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getVip()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getRegisterTime()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, shareInvitationData.getOpenId()));
        if (getDb().executeNoneQuery(updateShareInvitationDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 旧数据没有openId，需要用serverId跟userId更新openId进去
     */
    public boolean updateDataByOldKey(ShareInvitationData shareInvitationData) {
        String sql = "update `t_u_invite_user_data` set earnSpeed=?,vip=?,registerTime=?,openId=? where userId=? and invitationServerId=? and invitationUserId=?";
        shareInvitationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, shareInvitationData.getEarnSpeed().toString()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getVip()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getRegisterTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, shareInvitationData.getOpenId()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getInvitationServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getInvitationUserId()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertShareInvitationDataSql = "insert into `t_u_invite_user_data`(userId,invitationServerId,invitationUserId,earnSpeed,vip,registerTime,openId) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ShareInvitationData shareInvitationData) {
        shareInvitationData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getInvitationServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getInvitationUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, shareInvitationData.getEarnSpeed().toString()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getVip()));
        para.put(i++, new DbParameter(Types.BIGINT, shareInvitationData.getRegisterTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, shareInvitationData.getOpenId()));
        if (getDb().executeNoneQuery(insertShareInvitationDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ShareInvitationData parseData(ResultSet rs) throws SQLException {
        ShareInvitationData shareInvitationData = new ShareInvitationData();
        shareInvitationData.setUserId(rs.getLong("userId"));
        shareInvitationData.setInvitationServerId(rs.getLong("invitationServerId"));
        shareInvitationData.setInvitationUserId(rs.getLong("invitationUserId"));
        shareInvitationData.setEarnSpeed(new BigInteger(rs.getString("earnSpeed")));
        shareInvitationData.setVip(rs.getLong("vip"));
        shareInvitationData.setRegisterTime(rs.getLong("registerTime"));
        String openId = rs.getString("openId");
        if (!StringUtils.isNullOrEmpty(openId)) {
            shareInvitationData.setOpenId(openId);
        }
        return shareInvitationData;
    }
}
